package com.ykg.controller;

import com.ykg.annotation.PassToken;
import com.ykg.dto.EntrustDTO;
import com.ykg.enums.ResultEnum;
import com.ykg.interceptor.ScopeLevel;
import com.ykg.mapper.EntrustMapper;
import com.ykg.pojo.Entrust;
import com.ykg.request.CheckEntrustRequest;
import com.ykg.service.IEntrustService;
import com.ykg.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * TODO
 * 类(接口)：
 * Created on 16:43 2021/6/24.
 *
 * @Author: laobian
 * @Description:
 */
@Api(tags = "生产业务部货运员Api")
@RestController
@RequestMapping("/api/entrust")
public class EntrustController {
    @Autowired
    private IEntrustService entrustService;

   // @PassToken
    @ApiOperation("获取全部的委托单")
    @GetMapping(value = "/all",produces = "application/json")
    @ScopeLevel(value = 2)
    public ResultResponse findAllEntrust(){
        List<Entrust> entrusts = entrustService.getEntrust();
        return  new ResultResponse(entrusts);
    }

    //@PassToken
    @ApiOperation("获取某个委托单的详情")
    @GetMapping(value = "/{entrustId}",produces = "application/json")
    @ScopeLevel(value = 2)
    public ResultResponse findEntrustById(@PathVariable(value = "entrustId") String entrustId){
        if(entrustId==null){
            return  new ResultResponse(ResultEnum.PARAM_ERROR);
        }
        EntrustDTO entrustDTO = entrustService.entrustDetailed(entrustId);
        if(Objects.isNull(entrustDTO)){
            return new ResultResponse(ResultEnum.FAILURE);
        }
        return new ResultResponse(entrustDTO);
    }

    //@PassToken
    @ApiOperation("新增委托单")
    @PostMapping(value = "/addEntrust")
    @ScopeLevel(value = 2)
    public ResultResponse addEntrust(@RequestBody CheckEntrustRequest checkEntrustRequest){
        if(Objects.isNull(checkEntrustRequest)){
            return new ResultResponse(ResultEnum.PARAM_ERROR);
        }
        int status = entrustService.generateEntrust(checkEntrustRequest);
        if(status==0){
            return new ResultResponse(ResultEnum.FAILURE);
        }
        return new ResultResponse((ResultEnum.SUCCESS));

    }
}
